
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:osc/common/constants.dart';
import 'package:osc/common/enums.dart';
import 'package:osc/common/osc_user_manager.dart';
import 'package:osc/event/event.dart';
import 'package:osc/event/event_constant.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/dialog/publish_dialog.dart';
import 'package:osc/ui/pages/account/login_page.dart';
import 'package:osc/ui/pages/other/find.dart';
import 'package:osc/ui/pages/other/home.dart';
import 'package:osc/ui/pages/other/mine.dart';
import 'package:osc/ui/pages/post/deliver_post.dart';
import 'package:osc/ui/pages/post/publish_question.dart';
import 'package:osc/ui/pages/tweet/publish_tweet.dart';
import 'package:osc/ui/pages/tweet/tweet.dart';
import 'package:osc/utils/navigator_util.dart';
import 'package:osc/utils/pd_util.dart';


class NavigationPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new NavigationPageState();
  }
}

class NavigationPageState extends State<NavigationPage>
    with TickerProviderStateMixin {

  static const methodChannel = const MethodChannel(Constant.CUSTOMER_SHARE_SERVICE);

  TabController controller;
  Locale locale;
  Color themeColor = Colors.white;
  int tabIndex = 0;
  List<Widget> pages = [new Home(), new Tweet(), Text(""), new Find(), new Mine()];



  AnimationController animationController;
  CurvedAnimation curve;
  Animation<double> animation;
  bool showingPopWindow = false;

  @override
  void initState() {
    super.initState();
    controller = TabController(length: 5, vsync: this);
//    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);//状态栏显示出来
    SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top, SystemUiOverlay.bottom]);////状态栏和虚拟键显示出来
    onBusEvent();


    animationController = AnimationController(
        duration: const Duration(milliseconds: 1500), vsync: this, animationBehavior: AnimationBehavior.preserve);
    curve = CurvedAnimation(parent: animationController, curve: Curves.linear);
    animation = Tween(begin: 0.0, end: 0.125).animate(animationController);

    animationController.addListener((){
      if(animationController.isCompleted){
        animationController.reset();
      }
    });
  }

  @override
  void dispose() {
    controller.dispose();
    animationController?.dispose();
    offBusEvent();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: pages[tabIndex],
        bottomNavigationBar: Container(
          decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
              top: BorderSide(
                color: Colors.black26,
                width: Dimens.gap_divider_height_smallest,
              ),
            )
          ),
          child: TabBar(
              controller: controller,
              isScrollable: false,
              labelColor: Colors.green,
              labelPadding: EdgeInsets.zero,
              unselectedLabelColor: Colors.grey,//未选中项颜色
              indicator: ShapeDecoration(shape: Border.all(color: Colors.transparent)),//透明选中下标线
              indicatorPadding: EdgeInsets.zero,//底部指示器的Padding
              indicatorColor: Colors.green,
              indicatorSize: TabBarIndicatorSize.label,//下标线宽度和文字一样
              indicatorWeight: 0,
              onTap: (index){
                if(index == 2){
                  onTabSelect(tabIndex);
                  setState(() {
                    controller.index = tabIndex;
                  });
                } else {
                  onTabSelect(index);
                }
              },
              tabs: [
                getBottomNavigationBarItem2(0, "ic_nav_news_normal", "ic_nav_news_actived", "综合"),
                getBottomNavigationBarItem2(1, "ic_nav_tweet_normal", "ic_nav_tweet_actived", "猿圈"),
                getBottomNavigationBarItemCenter(),
                getBottomNavigationBarItem2(3, "ic_nav_discover_normal", "ic_nav_discover_actived", "广场"),
                getBottomNavigationBarItem2(4, "ic_nav_my_normal", "ic_nav_my_pressed", "我的"),
              ]
          ),
        ),
    );
  }

  getBottomNavigationBarItem(String iconName, String activeIconName, String title) {
    return BottomNavigationBarItem(
        icon: Image.asset(PdUtil.getImagePath(iconName), height: Dimens.view_item_icon_height_normal, fit: BoxFit.fitHeight),
        activeIcon: Image.asset(PdUtil.getImagePath(activeIconName), height: Dimens.view_item_icon_height_normal, fit: BoxFit.fitHeight),
        title: Text(title),
    );
  }

  getBottomNavigationBarItem2(int index, String iconName, String activeIconName, String title) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: Dimens.gap_view_small),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Image.asset(PdUtil.getImagePath(tabIndex == index?activeIconName:iconName), height: Dimens.view_item_icon_height_normal, fit: BoxFit.fitHeight),
          Offstage(
            child: Text(title),
            offstage: index == 2,
          )
        ],
      ),
    );
  }


  getBottomNavigationBarItemCenter(){
    return GestureDetector(
      onTap: (){
        showDialog(
            context: context,
            builder: (context){
              return Container(
                color: Colors.black12,
                child: PublishDialog(
                  onClickEvent: (id){
                    if(mounted){
                      Navigator.pop(context);
                    }
                    switch(id){
                      case PublishItemId.ID_CLOSE:
                        break;
                      case PublishItemId.ID_TWEET:
                        jump2Page(PublishTweet(), checkLogin: true);
                        break;
                      case PublishItemId.ID_QUESTION:
                        jump2Page(PublishQuestion(), checkLogin: true);
                        break;
                      case PublishItemId.ID_ARTICLE:
                        jump2Page(DeliverPost(), checkLogin: true);
                        break;
                    }

                  },
                ),
              );
            }
        );
        setState(() {
          animationController.animateTo(0.125);
        });
      },
      child: RotationTransition(
        turns: curve,
        child: Container(
          decoration: BoxDecoration(
            color: Colors.green,
            shape: BoxShape.circle,
          ),
          child: Padding(
            padding: EdgeInsets.all(Dimens.gap_view_big),
            child: Image.asset(
              PdUtil.getImagePath("icon_add_orange"),
              height: Dimens.view_item_icon_height_normal,
            ),
          ),
        ),
      ),
    );
  }

  onTabSelect(int index){
    setState(() {
      tabIndex = index;
    });
  }

  jump2Page(Widget page, {bool checkLogin = false}){
    if(checkLogin && !OscUserManager().isLogin()){
      NavigatorUtil.pushPage(context, LoginPage());
      PdUtil.showToast(msg: "您还没有登录");
    } else {
      NavigatorUtil.pushPage(context, page);
    }
  }

  onBusEvent(){
    eventBus.on(EventConstant.INDEX, (index){
      onTabSelect(index);
    });
    eventBus.on(EventConstant.LOGOUT, (tag){
      setState(() {

      });
    });
  }
  offBusEvent(){
    eventBus.off(EventConstant.INDEX);
    eventBus.off(EventConstant.LOGOUT);
  }
}
